package com.xingchen.springboot3.controller;

import com.xingchen.springboot3.common.bean.PageBean;
import com.xingchen.springboot3.common.bean.Result;
import com.xingchen.springboot3.pojo.dto.ArticleDto;
import com.xingchen.springboot3.pojo.entity.Article;
import com.xingchen.springboot3.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author weijingchen
 * @DateTime 2025-09-24
 */
@RestController
@RequestMapping("/article")
@RequiredArgsConstructor

public class ArticleController {

    private final ArticleService articleService;

    @PostMapping("/add")
    public Result add(@Validated(ArticleDto.add.class) @RequestBody ArticleDto dto) {
        articleService.add(dto);
        return Result.success();
    }
    @PostMapping("/update")
    public Result update(@Validated(ArticleDto.update.class) @RequestBody ArticleDto dto) {
        articleService.update(dto);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }
    @PostMapping("/list")
    public Result<PageBean<Article>> list(@RequestBody ArticleDto dto) {
        return Result.success(articleService.list(dto));
    }

    @GetMapping("/id")
    public Result<Article> getById(@RequestParam Integer id) {
        return Result.success(articleService.getById(id));
    }
}
